package com.zejson.controller;

import com.zejson.entity.Driver;
import com.zejson.service.DriverService;
import com.zejson.service.TruckTeamService;
import com.zejson.util.DateConvert;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.Scope;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.lang.reflect.InvocationTargetException;
import java.text.ParseException;
import java.util.Date;
import java.util.List;

@Controller             //Controller class
@Scope("prototype")	    //generate a new instance for new request
@RequestMapping("driver")
public class DriverController {

    @RequestMapping("")
    public ModelAndView index() throws ClassNotFoundException {
        ModelAndView model;
        ApplicationContext applicationContext = new ClassPathXmlApplicationContext("applicationContext.xml");
        TruckTeamService truckTeamService =  ((TruckTeamService)applicationContext.getBean("truckTeamService"));
        List teams = truckTeamService.getList();

        DriverService driverService = ((DriverService)applicationContext.getBean("driverService"));
        List drivers = driverService.getList();
        model = new ModelAndView("/templates/driver/driver");
        model.addObject("teams",teams);
        model.addObject("drivers",drivers);
        return model;
    }

    @RequestMapping("/add_driver")
    public ModelAndView add_driver(HttpServletRequest request, HttpServletResponse response) throws ClassNotFoundException, InvocationTargetException, NoSuchMethodException, IllegalAccessException {

        ModelAndView model;
        if(request.getMethod().equals("POST")){
            String Name = request.getParameter("Name");
            String Sex = request.getParameter("Sex");
            String Birth = request.getParameter("Birth");
            String Phone = request.getParameter("Phone");
            String IDCard = request.getParameter("IDCard");
            //String FK_TeamID = request.getParameter("teamId");
            String Remark = request.getParameter("Remark");

            Driver driver = new Driver();
            driver.setName(Name);
            driver.setSex((byte) Integer.parseInt(Sex));
            Birth = Birth.replaceAll("[-]", "");
            java.sql.Date Birth1 = new java.sql.Date((long)Integer.parseInt(Birth));
            driver.setBirth(Birth1);
            driver.setPhone(Phone);
            driver.setIdcard(IDCard);
            //driver.setFkTeamid(Integer.parseInt(teamId));
            driver.setState(0);
            Date utilDate  =new Date();
            java.sql.Date sqlDate = new java.sql.Date(utilDate.getTime());
            driver.setCheckintime(sqlDate);
            driver.setIsdeleted((byte)0);
            driver.setRemark(Remark);

            ApplicationContext applicationContext = new ClassPathXmlApplicationContext("applicationContext.xml");
            DriverService driverService =  ((DriverService)applicationContext.getBean("driverService"));
            driverService.insertByAutoGeneratePK("DriverMapper",driver);
        }
        model = new ModelAndView("/templates/driver/driver");
        return model;
    }

    @RequestMapping("/update_driver")
    public String updateDriver(HttpServletRequest request) throws ParseException, ClassNotFoundException {

        Integer driverId = Integer.parseInt(request.getParameter("driver-id"));
        Driver driver = new Driver();
        driver.setDriverid(driverId);
        driver.setName(request.getParameter("Name"));
        driver.setSex((byte) Integer.parseInt(request.getParameter("Sex")));
        driver.setBirth(DateConvert.parseDate(request.getParameter("Birth")));
        driver.setPhone(request.getParameter("Phone"));
        driver.setIdcard(request.getParameter("IDCard"));
        //driver.setString FK_TeamID = request.getParameter("FK_TeamID");
        driver.setRemark(request.getParameter("Remark"));
        driver.setIsdeleted((byte) 0);
        ApplicationContext applicationContext = new ClassPathXmlApplicationContext("applicationContext.xml");
        DriverService driverService = (DriverService) applicationContext.getBean("driverService");
        driverService.updateById("DriverMapper", driver);
        return "redirect:/driver";
    }

    @RequestMapping("/delete_driver")
    public String deleteDriver(HttpServletRequest request) throws ClassNotFoundException {

        String driverId = request.getParameter("driverId");
        ApplicationContext applicationContext = new ClassPathXmlApplicationContext("applicationContext.xml");
        DriverService driverService = (DriverService) applicationContext.getBean("driverService");
        driverService.deleteById("DriverMapper", Integer.parseInt(driverId));
        return "redirect:/driver";
    }

}
